package au.edu.adelaide.mips_soft_core.board_interface_pc.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import au.edu.adelaide.mips_soft_core.board_interface_pc.core.Device;

public class InfoPanel extends JPanel{
	JLabel[] cpuLabels = new JLabel[16];
	JLabel[] cpuFrequencies = new JLabel[16];
	
	JLabel[] memoryLabels = new JLabel[16];
	JLabel[] memorySize = new JLabel[16];
	
	UI ui;
	
	public InfoPanel(UI ui){
		super(new GridBagLayout());
		this.ui = ui;
		addComponent(this, new JLabel("CPUs"), 0, 0, 2, 1);
		for(int i = 0; i < 16; i++){
			cpuLabels[i] = new JLabel("CPU #" + i + " Frequency (Hz)");
			cpuFrequencies[i] = new JLabel(Integer.toString((i+1) * 1000));
			addComponent(this, cpuLabels[i], 0, i + 1, 1, 1);
			addComponent(this, cpuFrequencies[i], 1, i + 1, 1, 1);
			//cpuLabels[i].setVisible(false);
			//cpuFrequencies[i].setVisible(false);
		}
		addComponent(this, new JLabel("Memory Modules"), 0, 17, 2, 1);
		for(int i = 0; i < 16; i++){
			memoryLabels[i] = new JLabel("Memory Module #" + i + " Size (32bit word)");
			memorySize[i] = new JLabel(Integer.toString((i+1) * 100));
			addComponent(this, memoryLabels[i], 0, i + 18, 1, 1);
			addComponent(this, memorySize[i], 1, i + 18, 1, 1);
			//memoryLabels[i].setVisible(false);
			//memorySize[i].setVisible(false);
			
		}
		updatePanel();
	}
	
	void updatePanel(){
		{
			for(int i = 0; i < 16; i++){
				cpuLabels[i].setVisible(false);
				cpuFrequencies[i].setVisible(false);
			}
			for(int i = 0; i < 16; i++){
				memoryLabels[i].setVisible(false);
				memorySize[i].setVisible(false);
			}
		}
		if(this.ui.connectedDevice != null){
			for(int i = 0; i < this.ui.connectedDevice.numberOfCPUModules(); i++){
				cpuLabels[i].setVisible(true);
				cpuFrequencies[i].setText(Integer.toString(Device.lookUpFrequencyTableValue((byte)this.ui.connectedDevice.frequencyOfCPUModule((byte)i))));
				cpuFrequencies[i].setVisible(true);
			}
			for(int i = 0; i < this.ui.connectedDevice.numberOfMemoryModules(); i++){
				memoryLabels[i].setVisible(true);
				memorySize[i].setText(Integer.toString(this.ui.connectedDevice.sizeOfMemoryModule((byte)i)));
				memorySize[i].setVisible(true);
			}
		}
	}
	
	
	
	private void addComponent(JPanel panel, JComponent component, int x, int y, int width, int height){
		 GridBagConstraints c = new GridBagConstraints();
		 c.gridx = x;
		 c.gridy = y;
		 c.gridheight = height;
		 c.gridwidth = width;
		 panel.add(component,  c);
	}

}
